var dropDownNoScroll={
	liHeight:25,
	recCount:7,
	oblAttribute:"data-obligatary",
	currentElem:undefined,
	detectIE:function(){
		if (document.attachEvent)
		{
			this.isIE=true;
		}
		else
		{
			this.isIE=false;
		}
	},
	addEvent:function(target,eventName,funcName){
		this.detectIE();

		
		if (this.isIE)
			target.attachEvent("on"+eventName,funcName);
		else
		{
			target.addEventListener(eventName,funcName);
			if (eventName=='mousewheel')
				target.addEventListener('DOMMouseScroll',funcName);
		}
	},
	removeEvent:function(target,eventName,funcName){
		this.detectIE();

		if (this.isIE)
		target.detachEvent("on"+eventName,funcName);
		else
		{
		target.removeEventListener(eventName,funcName);
			if (eventName=='mousewheel')
			target.removeEventListener('DOMMouseScroll',funcName);
		}
	},
	init:function(className,recCount){
		if (recCount)
		this.recCount=recCount;
		if (className)
		this.className=className;
		else
		this.className="sl_dl_dropdown";
		this.addEvent(window,"load",function(){
			dropDownNoScroll.addEvent(document,"click",dropDownNoScroll.clickHandler)
			dropDownNoScroll.getAllBoxes();
		})
		this.addEvent(document,"click",dropDownNoScroll.errHandler);
	},
	getByClassName:function(className){
		var elements=new Array();
		if (dropDownNoScroll.isIE){
			var temp=document.getElementsByTagName("*");
			var elements=new Array();
			for (var i=0;i<temp.length;i++){
				if (temp[i].nodeType==1 && temp[i].className.indexOf(className)>-1)
				elements.push(temp[i]);
			}
		}
		else{
			elements=document.getElementsByClassName(className);
		}
		return elements;
	},
	getAllBoxes:function(){
		var elements=dropDownNoScroll.getByClassName(dropDownNoScroll.className);
		var first=false;
		for (var i=0; i<elements.length;i++)
		{
			var temp=elements[i];
			var defVal=dropDownNoScroll.defValue;
			
			temp.boxOpen=false;
			for (var j=0;j<temp.childNodes.length;j++){
				var tTemp=temp.childNodes[j];	
				if (tTemp.nodeType==1){
					if (tTemp.className.indexOf("sl_dl_drop_show")>-1)
					{
					temp.box=tTemp;
					temp.dropList=dropDownNoScroll.recClassSearch(tTemp,"sl_dl_drop_list")
					temp.count=dropDownNoScroll.countChildsByTag(temp.dropList,"LI");
					temp.slide=dropDownNoScroll.recClassSearch(tTemp,"sl_dl_slide");
					temp.show=dropDownNoScroll.recClassSearch(tTemp,"sl_dl_drop");		
					temp.slider=dropDownNoScroll.recClassSearch(temp.slide,"sl_dl_slider");
					temp.input=dropDownNoScroll.recClassSearch(temp.parentNode,"sl_dl_input");
						var cMath;
						if (temp.count<this.recCount)
						cMath=temp.count;
						else
						cMath=this.recCount;
						var  showBoxH=this.liHeight*cMath;
						temp.show.style.height=temp.slide.style.height=showBoxH+"px";
						var sliderH=cMath/temp.count*(this.liHeight*cMath);
						temp.slider.style.height=sliderH+"px";
						temp.slider.myHeight=sliderH;
						temp.slider.slideHeight=showBoxH;
						temp.slider.currentPos=0;
						temp.slider.grandParent=temp;
					}
					if (tTemp.tagName.toUpperCase()=="INPUT" && !first)
					{
					first=true;
					temp.input=tTemp;
					}
				}
			}
		}
		dropDownNoScroll.allBoxes=elements;
	},
	recClassSearch:function(parent,target,boola){
		for (var i=0;i<parent.childNodes.length;i++)
		{
			if (parent.childNodes[i].nodeType==1 && parent.childNodes[i].className.indexOf(target)>-1)
			{
				return parent.childNodes[i];
			}
			if (parent.childNodes[i].childNodes.length>0)
			{
				var atb=dropDownNoScroll.recClassSearch(parent.childNodes[i],target);
				if (atb!=-1)
				{return atb;}
			}
		}
		return -1;
	},
	countChildsByTag:function(element,tagname){
		var count=0;
		if (element.childNodes.length>0)
		{
			for (var i=0;i<element.childNodes.length;i++){
				if (tagname.toUpperCase=="LI" && i==0)
					dropDownNoScroll.liHeight=element.childNodes[i].offHeight;
				if (element.childNodes[i].nodeType==1 && element.childNodes[i].tagName.toUpperCase()==tagname)
				count++;
			}
			return count;
		}
		else
		return 0;
	},
	openBox:function(elem){
		if (elem)
		var temp=elem.boxOpen;
		function closeAll(){
			for (var i=0;i<dropDownNoScroll.allBoxes.length;i++){
				var dropBox=dropDownNoScroll.allBoxes[i];
				if (dropBox.boxOpen){
					dropBox.boxOpen=false;
					dropBox.box.style.display="none";
					dropDownNoScroll.removeEvent(dropBox.slider,"mousedown",dropDownNoScroll.onMouseDown);
					dropDownNoScroll.removeEvent(document,"mouseup",dropDownNoScroll.onMouseUp);
					dropDownNoScroll.removeEvent(document,"mousewheel",dropDownNoScroll.onMouseWheel);
					dropDownNoScroll.elemValidate(dropBox.input);
				}
			}
		}
		closeAll();
		if (elem)
		{
		if (!temp)
		{
			dropDownNoScroll.addEvent(elem.slider,"mousedown",dropDownNoScroll.onMouseDown);
			dropDownNoScroll.addEvent(document,"mouseup",dropDownNoScroll.onMouseUp);
			dropDownNoScroll.addEvent(document,"mousewheel",dropDownNoScroll.onMouseWheel);
			elem.box.style.display="block";
			dropDownNoScroll.currentElem=elem;
		}
		elem.boxOpen=!temp;
		}
		
	},
	onMouseWheel:function(event){
		event=event||window.event;

		var slider=dropDownNoScroll.currentElem.slider;
		var delta=event.wheelDelta?event.wheelDelta:event.detail*-40;
		var deltaY=-(dropDownNoScroll.liHeight/(dropDownNoScroll.liHeight*slider.grandParent.count/slider.slideHeight))*delta/Math.abs(delta);
		var height=(deltaY*(dropDownNoScroll.liHeight*slider.grandParent.count/slider.slideHeight))*event.wheelDelta/Math.abs(event.wheelDelta);
		if (slider.currentPos+deltaY>=0 && (slider.currentPos+deltaY)+slider.myHeight<=slider.slideHeight)
		{
			slider.style.top=(slider.currentPos+deltaY)+"px";
			slider.grandParent.dropList.style.top=-(slider.currentPos+deltaY)*dropDownNoScroll.liHeight*slider.grandParent.count/slider.slideHeight+"px";
		}
		if (slider.currentPos+deltaY<0)
		{
			slider.style.top=0+"px";
			slider.grandParent.dropList.style.top=0+"px";
		}
		if ((slider.currentPos+deltaY)+slider.myHeight>slider.slideHeight)
		{
			slider.style.top=slider.slideHeight-slider.myHeight+"px";
			slider.grandParent.dropList.style.top=-(slider.slideHeight-slider.myHeight)*dropDownNoScroll.liHeight*slider.grandParent.count/slider.slideHeight+"px";
		}
		slider.currentPos=parseInt(slider.style.top.substr(0,slider.style.top.length-2));
		
		if (event.preventDefault)
		event.preventDefault();
		event.returnValue=false;
		event.cancelBubble=true;
		return false;
	},
	onMouseDown:function(event){
		var target;
		if (dropDownNoScroll.isIE)
			target=event.srcElement;
		else
			target=event.target;
			
		dropDownNoScroll.currentDrag=target;
		dropDownNoScroll.addEvent(document,"mousemove",dropDownNoScroll.onMouseMove);
		dropDownNoScroll.addEvent(document,"selectstart",dropDownNoScroll.preventSelection);
		if (!dropDownNoScroll.isIE)
		event.preventDefault();
		var str,d=1;
		dropDownNoScroll.startY=event.screenY;
		dropDownNoScroll.innerStartY=event.offsetY;
		if (event.preventDefault)
		event.preventDefault();
		return false;
	},
	preventSelection:function(){
		return false;
	},
	onMouseUp:function(event){
		if (dropDownNoScroll.currentDrag)
		{
			dropDownNoScroll.removeEvent(document,"mousemove",dropDownNoScroll.onMouseMove);
			dropDownNoScroll.removeEvent(document,"selectstart",dropDownNoScroll.preventSelection);
		dropDownNoScroll.currentDrag.currentPos=parseInt(dropDownNoScroll.currentDrag.style.top.substr(0,dropDownNoScroll.currentDrag.style.top.length-2));
		}
	},
	onMouseMove:function(event){
		var slider=dropDownNoScroll.currentDrag;
		var deltaY=event.screenY-dropDownNoScroll.startY;

		if (slider.currentPos+deltaY>=0 && (slider.currentPos+deltaY)+slider.myHeight<=slider.slideHeight)
		{
		slider.style.top=(slider.currentPos+deltaY)+"px";
		slider.grandParent.dropList.style.top=-(slider.currentPos+deltaY)*dropDownNoScroll.liHeight*slider.grandParent.count/slider.slideHeight+"px";
		}
		if (slider.currentPos+deltaY<0)
		{
		slider.style.top=0+"px";
		slider.grandParent.dropList.style.top=0+"px";
		}
		if ((slider.currentPos+deltaY)+slider.myHeight>slider.slideHeight)
		{
		slider.style.top=slider.slideHeight-slider.myHeight+"px";
		slider.grandParent.dropList.style.top=-(slider.slideHeight-slider.myHeight)*dropDownNoScroll.liHeight*slider.grandParent.count/slider.slideHeight+"px";
		}
		
		
	},
	
	setValue:function(string){
		for (var i=0;i<dropDownNoScroll.allBoxes.length;i++){
			if (dropDownNoScroll.allBoxes[i].boxOpen){
				dropDownNoScroll.allBoxes[i].input.value=string;
			}
		}
		dropDownNoScroll.openBox();
	},
	
	clickHandler:function(event){
		var target;
		
		if (dropDownNoScroll.isIE)
			target=event.srcElement;
		else
			target=event.target;

		if (target.tagName.toUpperCase()=="INPUT" || target.className.indexOf("sl_dl_image")>-1)
		{
			if(target.parentNode.className.indexOf(dropDownNoScroll.className)>-1)
			{
				dropDownNoScroll.openBox(target.parentNode);
			}
			else
				dropDownNoScroll.openBox();
		}
		else if 
		(target.tagName.toUpperCase()=="LI")
		{
			var liis=target.parentNode.childNodes;
			for (var i=0; i<liis.length; i++){
				if (liis[i].nodeType==1)
				liis[i].style.cssText='';
			}
			
			target.style.background="#FC9707";
			target.style.boxShadow="inset 0 0 4px 1px black";
			var mainParent=target.parentNode.parentNode.parentNode.parentNode.parentNode;
			if ( mainParent.className.indexOf(dropDownNoScroll.className)>-1){
				var target=event.srcElement||event.target;
				var iid =target.attributes[0].value;
				$(dropDownNoScroll.currentElem).find('input')[1].value=iid;
				$(dropDownNoScroll.currentElem).find('input')[0].value=target.innerHTML;

				dropDownNoScroll.openBox();
				// dropDownNoScroll.setValue(target.innerHTML);
				var allLis=target.parentNode.getElementsByTagName("LI");
				// for (var k=0;k<allLis.length;k++){
					// if (allLis[k]===target)
					// {
						// var selectBox=dropDownNoScroll.recClassSearch(mainParent,"sl_dl_select_box");
						// if (selectBox.options.length>k)
						// selectBox.options[k].selected=true;
						// else
						// selectBox.options[0].selected=true;
					// }
				// }
			}
		}
		else if 
		(target.className.indexOf("sl_dl_slider")>-1 || target.className.indexOf("sl_dl_drop")>-1 || target.className.indexOf("sl_dl_slide")>-1)
		{
		}
		else
		dropDownNoScroll.openBox();
	},
	formValidate:function(form,attribute){
		var valid=true;
		if (form)
		{
			for (var i=0;i<form.elements.length;i++)
			{
				element=form.elements[i];
				if (element.tagName.toUpperCase()=="INPUT" || element.tagName.toUpperCase()=="TEXTAREA")
				{
					if (!dropDownNoScroll.elemValidate(element,attribute))
					{
						valid=false;
					}
				}
			}
		}
		return valid;
	},
	errHandler:function(event){
		if (event.srcElement)
		target=event.srcElement;
		else
		target=event.target;
		
		if 
		(target.className && (target.className.indexOf("sl_dl_err")>-1 || target.className.indexOf("sl_dl_text_cnt")>-1))
		{
			dropDownNoScroll.recDelete(target);

		}
	},
	recDelete:function(childEl){
		if (childEl.className.indexOf("sl_dl_err_cnt")>-1)
		childEl.parentNode.removeChild(childEl);
		else
		this.recDelete(childEl.parentNode);
	},
	elemValidate:function(element,attribute){
		if (!attribute)
		var attribute=dropDownNoScroll.oblAttribute;
		var blank="Text input is blank! sdf sdfsdfsdff sfdf dfsfdfdfsd ffd";
		if (element.getAttribute(attribute) && element.value.length==0)
		{
			var err=dropDownNoScroll.prepareElem(blank);
			if (element.parentNode.style.position && dropDownNoScroll.recClassSearch(element.parentNode,"sl_dl_err_cnt")==-1)
			{
				
				element.parentNode.appendChild(err);
				
			}
			else if (dropDownNoScroll.recClassSearch(element.parentNode.parentNode,"sl_dl_err_cnt")==-1)
			{
				element.parentNode.parentNode.appendChild(err);
				
			}
			var width=err.offsetWidth;
			while (err.div.offsetHeight>35){
				++width;
				err.style.width=width+"px";
			}
			
			
		return false;
		}
		else
		{
			if (dropDownNoScroll.recClassSearch(element.parentNode,"sl_dl_err_cnt")!=-1)
			dropDownNoScroll.recDelete(dropDownNoScroll.recClassSearch(element.parentNode,"sl_dl_err_cnt"));
			else if (dropDownNoScroll.recClassSearch(element.parentNode.parentNode,"sl_dl_err_cnt")!=-1)
			dropDownNoScroll.recDelete(dropDownNoScroll.recClassSearch(element.parentNode.parentNode,"sl_dl_err_cnt"));
		return true;
		}
	},
	prepareElem:function(string){
		var cnt=document.createElement("div");
		cnt.setAttribute("class","sl_dl_err_cnt");
		var err=document.createElement("div");
		err.setAttribute("class","sl_dl_err");
		var img=document.createElement("div");
		img.setAttribute("class","sl_dl_err_im");
		var tr=document.createElement("div");
		tr.setAttribute("class","sl_dl_err_tr");
		var div=document.createElement("div");
		div.setAttribute("class","sl_dl_text_cnt");
		var text=document.createTextNode(string);
		div.appendChild(text);
		err.appendChild(img);
		err.appendChild(div);
		cnt.appendChild(err);
		cnt.appendChild(tr);
		cnt.div=div;
		return cnt;
	}
}